package com.sctvcloud.longchang.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.longchang.R;
import com.sctvcloud.longchang.beans.ActivityBean;
import com.sctvcloud.longchang.beans.VoiceTemplateBean;
import com.sctvcloud.longchang.ui.utils.GlideUtil;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动栏目列表适配
 */
public class ActivityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ActivityBean> activityBeanList = new ArrayList<>();
    private Context ctx;
    private OnItemBtnClick listener;

    public ActivityAdapter(Context ctx, List<ActivityBean> lList, OnItemBtnClick listener) {
        this.ctx = ctx;
        this.activityBeanList = lList;
        this.listener = listener;
    }

    static class ActivityViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        CustomFontTextView name;
        CustomFontTextView time;
        LinearLayout itemView;

        public ActivityViewHolder(View view) {
            super(view);
            itemView = (LinearLayout) view.findViewById(R.id.item_view);
            img = (ImageView) view.findViewById(R.id.activity_img);
            name = (CustomFontTextView) view.findViewById(R.id.activity_name);
            time = (CustomFontTextView) view.findViewById(R.id.activity_time);

        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(ctx).inflate(R.layout.item_activity_list, parent, false);
        holder = new ActivityViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (null != activityBeanList && activityBeanList.size() > position) {
            final ActivityBean bean = activityBeanList.get(position);
            ActivityViewHolder h = (ActivityViewHolder) holder;
            h.name.setText(bean.getNewsTitle());
            if(!TextUtils.isEmpty(bean.getActstart())&&!TextUtils.isEmpty(bean.getActend())) {
                h.time.setText("活动时间：" + bean.getActstart() + " 至 " + bean.getActend());
            }else{
                h.time.setText("活动时间：");
            }
            GlideUtil.getGlideWithLarge169CenterCrop(ctx, bean.getNewsImage()).into(h.img);
            h.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener) {
                        listener.onItemBtnClick(bean);
                    }
                }
            });
        }
    }

    public void setDataList(List<ActivityBean> list) {
        this.activityBeanList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (null != activityBeanList) {
            return activityBeanList.size();
        } else {
            return 0;
        }
    }

    public interface OnItemBtnClick {
        void onItemBtnClick(ActivityBean bean);
    }
}

